
# 导入django model
import os
import sys

BASEDIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, BASEDIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'hshims.settings'

import django
django.setup()

from django.conf import settings
from django.apps import apps
from django.db.models.fields.related import ManyToOneRel


row = '''<el-row>
        {}
    </el-row>
'''
el_form = '''<el-col :span="12">
        <el-form-item label="{verbose_name}" prop="{name}">
            <el-input
                type="String"
                v-model="form.{name}"
                placeholder="{verbose_name}"
              />
        </el-form-item>
    </el-col>
'''

if __name__ == '__main__':
    args = sys.argv
    if len(args) == 1:
        print("请输入要转化的appName")
        exit()

    app_name = args[1]
    app_config = apps.get_app_config(app_name)
    all_models = app_config.get_models()
    print(all_models)

    for model in all_models:
        fields = model._meta.get_fields()
        print(model._meta.db_table)
        file_name = "{}.vue".format(model._meta.db_table)
        with open(file_name, 'w', encoding='utf-8') as f:
            temp_col = ''
            temp_row = ''
            lens = len(fields)
            for index, field in enumerate(fields):
                if isinstance(field, ManyToOneRel):
                    continue

                name = field.name
                verbose_name = field.verbose_name
                temp_form = el_form.format(name=name, verbose_name=verbose_name)
                temp_col += temp_form
                if index % 2 == 1:
                    temp_row += row.format(temp_col)
                    temp_col = ''
                elif index == lens - 1 and index % 2 == 0:
                    temp_row += row.format(temp_col)
                    temp_col = ''

            # print(temp_row)
            f.write(temp_row)

